#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt

def calc_statics(data):
    avg = np.mean(data)
    std = np.std(data)
    min = np.min(data)
    max = np.max(data)
    return {'avg':avg, 'std':std, 'min': min, 'max':max}

def main(logfile):
    data = np.loadtxt(logfile, delimiter=',', skiprows=1)/1000.
    detect = calc_statics(data[:,0])
    update = calc_statics(data[:,1])
    if data.shape[1] > 2:
        detect_dsp = calc_statics(data[:,2])
        update_dsp = calc_statics(data[:,3])
    plt.figure()
    plt.plot(data[:,0], '-', label='avg:{:.3f}\nstd:{:.3f}\nmin:{:.3f}\nmax:{:.3f}'.format(detect['avg'], detect['std'], detect['min'], detect['max']))
    plt.title('detect_cpu')
    plt.ylabel('ms')
    plt.legend()

    plt.figure()
    plt.plot(data[:,1], '-', label='avg:{:.3f}\nstd:{:.3f}\nmin:{:.3f}\nmax:{:.3f}'.format(update['avg'], update['std'], update['min'], update['max']))
    plt.title('update_cpu')
    plt.ylabel('ms')
    plt.legend()
    if data.shape[1] > 2:
        plt.figure()
        plt.plot(data[:,2], '-', label='avg:{:.3f}\nstd:{:.3f}\nmin:{:.3f}\nmax:{:.3f}'.format(detect_dsp['avg'], detect_dsp['std'], detect_dsp['min'], detect_dsp['max']))
        plt.title('detect_dsp')
        plt.ylabel('ms')
        plt.legend()

        plt.figure()
        plt.plot(data[:,3], '-', label='avg:{:.3f}\nstd:{:.3f}\nmin:{:.3f}\nmax:{:.3f}'.format(update_dsp['avg'], update_dsp['std'], update_dsp['min'], update_dsp['max']))
        plt.title('update_dsp')
        plt.ylabel('ms')
        plt.legend()
 
    plt.show()

if __name__ == '__main__':
    import sys
    # main(sys.argv[1])
    main('/home/yysh/dataset/qcomm_controller/dsp_out/log.txt')
    # main('/home/yysh/dataset/qcomm_controller/cpu_out/log.txt')
